import type { ModelRef } from 'vue';

import type {
  DialogModel,
  NdeCommissionNdeCommission,
  NdeCommissionStatus,
  TableOperateType,
  flowable
} from '@/index.d';
import { api, getLoginUserInfo, roles, updateOne, urls, useLoginUserStore } from '@/index';

export const sendWeixinMsg = async (msg: string) => {
  const res = await api({ method: 'get', url: '/dingding/send/' + msg });
  ElMessage.success(res.data);
};
export const reSetDialogModel = (dialogModel: ModelRef<DialogModel<object>, string>) => {
  dialogModel.value.id = 0;
  dialogModel.value.type = 'view';
  dialogModel.value.visible = false;
};

export const getReadonly = (dialogModel: DialogModel<object>) => {
  const readonly = reactive({ insertForbidden: true, updateForbidden: true });
  watchEffect(() => {
    switch (dialogModel.type) {
      case 'view':
        readonly.insertForbidden = true;
        readonly.updateForbidden = true;
        break;
      case 'insert':
        readonly.insertForbidden = false;
        readonly.updateForbidden = false;
        break;
      case 'update':
        readonly.insertForbidden = true;
        readonly.updateForbidden = false;
        break;
    }
  });
  return toRefs(readonly);
};

export const getReadonly2 = (type: Ref<TableOperateType>) => {
  const readonly = reactive({ insertForbidden: true, updateForbidden: true });
  switch (type.value) {
    case 'view':
      readonly.insertForbidden = true;
      readonly.updateForbidden = true;
      break;
    case 'insert':
      readonly.insertForbidden = false;
      readonly.updateForbidden = false;
      break;
    case 'update':
      readonly.insertForbidden = true;
      readonly.updateForbidden = false;
      break;
  }
  return toRefs(readonly);
};

//将英文字符串的首字母改为大写
export const capitalizeFirstLetter = (str: string): string => {
  return str.replace(/^./, match => match.toUpperCase());
};

export const isSuperAdmin = (): boolean => {
  const { loginUser } = useLoginUserStore();
  return !!loginUser.permissions && loginUser.permissions.includes(roles.superAdmin);
};

export const isAdmin = (): boolean => {
  const { loginUser } = useLoginUserStore();
  return !!loginUser.permissions && loginUser.permissions.includes(roles.admin);
};

export const isLoginUserById = (id: number): boolean => {
  const { loginUser } = useLoginUserStore();
  return loginUser.id === id;
};

export const isLoginUserByUsername = (username: string): boolean => {
  const { loginUser } = useLoginUserStore();
  return loginUser.username === username;
};

export const hasPermission = (permission: string): boolean => {
  const { loginUser } = useLoginUserStore();
  return !!loginUser.permissions && loginUser.permissions.includes(permission);
};

export const hasUpdatePermission = (permission: string): boolean => {
  const { loginUser } = useLoginUserStore();
  return !!loginUser.permissions && loginUser.permissions.includes('修改' + permission);
};

export const hasRole = (role: string): boolean => {
  const { loginUser } = useLoginUserStore();
  return !!loginUser.permissions && loginUser.permissions.includes(role);
};

export const setDeploymentVO = (target: flowable.DeploymentVO, source: flowable.DeploymentVO) => {
  target.deploymentId = source.deploymentId;
};

export const setProcessDefinitionVO = (
  target: flowable.ProcessDefinitionVO,
  source: flowable.ProcessDefinitionVO
) => {
  setDeploymentVO(target, source);
  target.processDefinitionId = source.processDefinitionId;
};

export const setProcessInstanceVO = (
  target: flowable.ProcessInstanceVO,
  source: flowable.ProcessInstanceVO
) => {
  setProcessDefinitionVO(target, source);
  target.processInstanceId = source.processInstanceId;
};

export const setExecutionVO = (target: flowable.ExecutionVO, source: flowable.ExecutionVO) => {
  setProcessInstanceVO(target, source);
  target.executionId = source.executionId;
};

export const setTaskInstanceVO = (
  target: flowable.TaskInstanceVO,
  source: flowable.TaskInstanceVO
) => {
  setExecutionVO(target, source);
  target.taskId = source.taskId;
};

export const changeNdeCommissionStatus = async (
  id: number,
  to: NdeCommissionStatus,
  callback: Function
) => {
  await updateOne<NdeCommissionNdeCommission>(urls.ndeCommissionNdeCommission, {
    id,
    ownerId: to === '执行委托' ? getLoginUserInfo().loginUserId : undefined,
    commissionStatus: to,
    updaterId: getLoginUserInfo().loginUserId
  });
  callback();
};
